package net.azure.app.view.menu;

import net.azure.app.model.AzureSession;
import net.azure.app.Azure358;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.io.File;

/**
 * Created by IntelliJ IDEA.
 * User: Venkatraman Kandaswamy : kvenkatraman@gmail.com
 * Date: 22-Jul-2008
 * Time: 06:33:38
 */
public class AzureMenu {
    private AbstractAction newItem = new NewAction("New...");
    private AbstractAction openItem = new OpenAction("Open...");
    private JMenuItem saveItem = new JMenuItem("Save");
    private JMenuItem printItem = new JMenuItem("Print...");
    private JMenuItem cutItem = new JMenuItem("Cut");
    private JMenuItem copyItem = new JMenuItem("Copy");
    private JMenuItem pasteItem = new JMenuItem("Paste");
    private JMenuBar menuBar = new JMenuBar();

    public JMenuBar buildMenuBar() {
        JMenu menu;
        menu = new JMenu("File");
        menu.add(newItem);
        menu.add(openItem);
        menu.add(saveItem);
        menu.addSeparator();
        menu.add(printItem);
        menuBar.add(menu);

        menu = new JMenu("Edit");
        menu.add(cutItem);
        menu.add(copyItem);
        menu.add(pasteItem);
        menuBar.add(menu);
        return menuBar;
    }

    private class NewAction extends AbstractAction {
        private NewAction(String name) {
            super(name);
        }

        public void actionPerformed(ActionEvent e) {
            AzureSession.reset();
        }
    }

    private class OpenAction extends AbstractAction {
        public OpenAction(String name) {
            super(name);
        }

        public void actionPerformed(ActionEvent e) {
            JFileChooser fileChooser = new JFileChooser();
            int returnValue = fileChooser.showOpenDialog(null);
            File selectedFile = null;
            if (returnValue == JFileChooser.APPROVE_OPTION) {
                selectedFile = fileChooser.getSelectedFile();
                Azure358.loadSession(selectedFile);
                AzureSession.getAzure().createUIComponents();
            }
        }
    }
}
